<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkModelTest')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

/** 
 *  This is base class to test PDK models using SimpleTest framework via SimpleTest plugin
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
include_spip('classes/simpletest/class.PdkTest');
class PdkModelTest extends PdkTest {
    /**
     *  Model wich will be unit tested
     */
    protected $model;
    
    public function __construct($name = false) {
        parent::__construct($name);                        
    }
    
    /**
     *  Run after all tests
     */    
    public function __destruct() { 
        parent::__destruct();
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        if (!isset($this->model)) throw new PdkException("PDK model for unit testing wasn't set!");
        parent::setUp();
    }
    
    /**
     *  Set PDK Model for unit testing
     */
    public function SetPdkModel($name, pdk_Model $model) {
        $this->model = $model;
        $this->AddPdkModel($name, $model);
    }
    
   function Test_form_data_from_array() {
        $data_check = $this->model->form_data_from_array($this->model->test_data[0]);

        $data_changed = $data_check; 
        $data_changed['added_field'] = 'test!!!';
        
        $data_filtered = $this->model->form_data_from_array($data_changed);
        
        $this->assertEqual($data_check, $data_filtered);
    } 
    
   function Test_Insert() {
        $this->model->delete();
    
        $data = $this->model->test_data[0];
        //$data[$this->model->primary_key] = 999;
        $this->model->insert($data);
        // Check main table
        $sel_data = sql_fetsel('*',$this->model->main_table);
        // Filter data, and select only main fields to assert main table
        foreach ($this->model->main_fields as $field=>$alias) {
            $data_check_sel[$alias] = $sel_data[$field];
            $data_check[$alias] = $data[$alias];
        }
        $this->assertEqual($data_check, $data_check_sel);
        
        // Check join tables
        foreach ($this->model->joins as $table_name=>$join_data) {
            $sel_data_join = sql_fetsel('*',$table_name, $join_data['join_id'].'='.$sel_data[$join_data['main_id']]);
            $data_check_sel = array(); $data_check = array();
            
            foreach ($join_data['fields'] as $field=>$alias) {
                $data_check_sel[$alias] = $sel_data_join[$field];
                $data_check[$alias] = $data[$alias];
            }
            $this->assertEqual($data_check, $data_check_sel);
        }                                      
	}
    
    /**
     *  Test get_total method
     */
    function Test_GetTotal() {  //exit;       
        $check_total = count($this->model->test_data);
        $this->assertEqual($check_total, $this->model->get_total());        
    }
    
    function Test_Select() {        
        $data_sel = $this->model->select($this->model->main_table.'.'.$this->model->primary_key.' > 0');
        $this->assertEqual($this->model->get_total() , count($data_sel));
    }
    
    function Test_SelectById() {
        $data_check = $this->model->form_data_from_array($this->model->test_data[0]);
        $data_sel = $this->model->form_data_from_array($this->model->select_by_id($data_check[$this->model->primary_key]));
        $this->assertEqual($data_check, $data_sel);
    }
    
    // Add tests for update, delete and validate functions
}
?>